#include "TestFramework.h"
#include "API/WebCore.h"

#define WIDTH	600
#define HEIGHT	600
#define LENGTH_SEC	1

class Test_DivLayerOpacity : public Test, public Awesomium::WebViewListener
{
	Awesomium::WebView* webView;
  bool _hasLoaded;

public:
	Test_DivLayerOpacity() : Test("DivLayerOpacity")
    , _hasLoaded(false)
	{
		webView = Awesomium::WebCore::Get().createWebView(WIDTH, HEIGHT);
    webView->setListener(this);
		webView->loadFile("tests/DivOpacityTest.html");
    while (!_hasLoaded)
    {
      Awesomium::WebCore::Get().update();
      SleepWait(10);
    }
	}

	~Test_DivLayerOpacity()
	{
		webView->destroy();
	}

	bool run()
	{
		log("Running");

		unsigned char* buffer = new unsigned char[WIDTH * HEIGHT * 4];

		timer t;
		t.start();
		int loopCount = 0;
		int renderCount = 0;

 		while(t.elapsed_time() < LENGTH_SEC)
		{
      {
        webView->executeJavascript("create_transparent_layer();");
        webView->syncLayout();

       if (!webView->isDirty())
       {
          log("Test failed, webview should be dirty to paint the transparent div.");
          std::cout << "loopCount " << loopCount << std::endl;
          return false;
       }

			  renderCount++;
			  webView->render(buffer, -1, Awesomium::WebView::RCM_UPDATE_ALL);

        unsigned char pixel_b = buffer[0];
        unsigned char pixel_g = buffer[1];
        unsigned char pixel_r = buffer[2];

        if (! (pixel_r == 128 && pixel_g == 0 && pixel_b == 0))
        {
			    log("Test failed, incorrect pixel color (expected half bright red)");
			    return false;
        }

        webView->syncLayout();

        if (webView->isDirty())
         {
            log("Test failed, webview should not be dirty, we've just painted it.");
            std::cout << "loopCount " << loopCount << std::endl;
            return false;
         }
      }
      {
        webView->syncExecuteJavascript("opacify_layer();");

        if (!webView->isDirty())
        {
			    log("Test failed, webview should be dirty to redraw the transparent layer.");
          std::cout << "loopCount " << loopCount << std::endl;
			    return false;
        }

			  renderCount++;
			  webView->render(buffer, -1, Awesomium::WebView::RCM_UPDATE_DIRTY);

        unsigned char pixel_b = buffer[0];
        unsigned char pixel_g = buffer[1];
        unsigned char pixel_r = buffer[2];

        if (! (pixel_r == 64 && pixel_g == 0 && pixel_b == 0))
        {
			    log("Test failed, incorrect pixel color (expected red)");
			    return false;
        }
      }
      {
        webView->syncExecuteJavascript("delete_layer();");

        if (!webView->isDirty())
        {
			    log("Test failed, webview should be dirty to paint in red.");
          std::cout << "loopCount " << loopCount << std::endl;
			    return false;
        }

			  renderCount++;
			  webView->render(buffer, -1, Awesomium::WebView::RCM_UPDATE_ALL);

        unsigned char pixel_b = buffer[0];
        unsigned char pixel_g = buffer[1];
        unsigned char pixel_r = buffer[2];

        if (! (pixel_r == 255 && pixel_g == 0 && pixel_b == 0))
        {
			    log("Test failed, incorrect pixel color (expected red)");
			    return false;
        }
      }

      loopCount++;
			SleepWait(1);
		}

		delete[] buffer;

		logTestValue("RenderSync_LoopCount", loopCount / (double)LENGTH_SEC);
		logTestValue("RenderSync_RenderCount", renderCount / (double)LENGTH_SEC);

		return true;
	}

  void onBeginNavigation(const std::string& url, const std::wstring& frameName) {}
	void onBeginLoading(const std::string& url, const std::wstring& frameName, int statusCode, const std::wstring& mimeType) {}
	void onFinishLoading() { _hasLoaded = true; }
	void onCallback(const std::string& name, const Awesomium::JSArguments& args) {}
	void onReceiveTitle(const std::wstring& title, const std::wstring& frameName) {}
	void onChangeTooltip(const std::wstring& tooltip) {}
#if defined(_WIN32)
	void onChangeCursor(HCURSOR cursor) {}
#endif
	void onChangeKeyboardFocus(bool isFocused) {}
	void onChangeTargetURL(const std::string& url) {}
	void onCreateNewWebView() {};
};